﻿using System;
using System.Diagnostics;
using System.Diagnostics.Eventing;

namespace QND
{
    //Known bugs
    // http://connect.microsoft.com/VisualStudio/feedback/details/712802/eventprovidertracelistener-doesnt-include-eventid-and-activityid-in-the-events 
    // Sample USage
    /*
                Tracer.source.TraceData(TraceEventType.Warning | TraceEventType.Start, 2,
                new object[] { "QND Enter Data Item Encoder", "def", true, 123 });
     * The only difference I can see between TraceData and TraceEvent is that TraceData logs in debug (i.e. Debug.Print) the string payload, while TraceEvent don't
            Tracer.source.TraceEvent(TraceEventType.Verbose, 1);
            Tracer.source.TraceEvent(TraceEventType.Information, 3);
            Tracer.source.TraceEvent(TraceEventType.Error, 4);
            Tracer.source.TraceEvent(TraceEventType.Critical, 5);
            Tracer.source.TraceEvent(TraceEventType.Warning | TraceEventType.Start, 6, "Fromatted string {0}", "param");
    */
    internal class Tracer
    {
        static private EventProviderTraceListener listener;
        static Guid providerGUID = new Guid("{E4A69BBA-7AC8-40E1-832A-4B6996291D38}");
        static internal TraceSource source;
        static Tracer()
        {
            Debug.Print("QND-Entering Tracer Constructor");
            Tracer.listener = new EventProviderTraceListener(providerGUID.ToString(),"QND.SCOMMM.Provider");
            Tracer.listener.TraceOutputOptions = Tracer.listener.TraceOutputOptions | TraceOptions.Timestamp;
            Tracer.source = new TraceSource("QND.SCOMMM", SourceLevels.All);
            Tracer.source.Listeners.Add(Tracer.listener);
            Debug.Print("QND-exiting Tracer constructor");
        }

        ~Tracer()
        {
            Debug.Print("QND-Entering Tracer Destructor");
            Tracer.source.Close();
        }
    }
}
